﻿using ACC.Business;
using ACC.Business.Stn;
using ACC.Data;
using ACC.Data.ACCLog;
using ACC.Data.Comm;
using ACC.Data.Line;
using ACC.Device;
using ACC.LineDevice.KukaPack;
using ACC.PrintTemplate;
using ACC.Interface.Extensions;
using System;
using ACC.Business.Utils;

namespace Script
{
    /// <summary>
    /// 模组搬运
    /// 打印模组国标码标签 (已改成PLC打印）
    /// 机器人抓取模组发进站请求，托盘出站发出站请求（可能托盘堵死，但是机器人还是会抓取模组，但是不出站）
    /// </summary>
    public class L1M04030 : StnProcessKukaPack
    {
        public L1M04030(StnRunningData data)
            : base(data)
        {
        }

        protected override ACCDevice CreateDevice()
        {

            ACCDevice dev = base.CreateDevice();
            if (kukaSiemensDev != null)
            {
                switch (_data.StnCfg.Name)
                {
                    case "L1M04031":
                        kukaSiemensDev.DB190_Offset = 15000;
                        break;
                    case "L1M04032":
                        kukaSiemensDev.DB190_Offset = 16000;
                        break;
                }

                kukaSiemensDev.DB190 = 800;
                kukaSiemensDev.DB192 = 80;
                kukaSiemensDev.DB193 = 800;
                kukaSiemensDev.DB194 = 800;
                kukaSiemensDev.DB192_PLC_Offset = (ushort)(kukaSiemensDev.DB190_Offset + 66); // 15066;
                kukaSiemensDev.DB193_PLC_Offset = (ushort)(kukaSiemensDev.DB190_Offset + 34);  // 15034;
                kukaSiemensDev.DB193_ACC_Offset = (ushort)(kukaSiemensDev.DB190_Offset + 68);  // 15068;
                kukaSiemensDev.DB194_PLC_Offset = (ushort)(kukaSiemensDev.DB190_Offset + 34);  // 15034;
                kukaSiemensDev.DB194_PLC_Data_Offset = (ushort)(kukaSiemensDev.DB190_Offset + 78); // 15078;
            }

            return dev;
        }

        protected override void CreateBusinessLogic()
        {
            base.CreateBusinessLogic();

            _loadProcess.Uninit();
            _loadProcess = new LoadWithAutoUnload(_data);
        }

        public override ACCCode ReadLoadParam(ACCDevice dev, out ACCDevice.LoadParam param)
        {
            PalletNo = string.Empty;
            IsReport = true;
            ACCCode rs = base.ReadLoadParam(dev, out param);

            return rs;
        }

        public override ACCCode SendLoadResult(ACCDevice dev, ACCDevice.LoadResult result)
        {
            // 2020/4/25 KUKA 张建平，朱德清要求进站永远OK
            if (result.ErrorCode == ACCCode.ACCOK)
                IsReport = true;
            else
                IsReport = false;

            result.ErrorCode = ACCCode.ACCOK;
            result.ErrorMsg = string.Empty;

            return base.SendLoadResult(dev, result);
        }

        public override ACCCode ReadUnloadParam(ACCDevice dev, out ACCDevice.UnloadParam param)
        {
            PalletNo = string.Empty;
            ACCCode rs = base.ReadUnloadParam(dev, out param);
            if (!string.IsNullOrEmpty(PalletNo))
            {
                this.RemovePallet(PalletNo);
            }
            rs = ACCCode.ACCInternalError; // 随便给个失败原因，后面再给PLC OK

            return rs;
        }

        public override ACCCode SendUnloadResult(ACCDevice dev, ACCDevice.UnloadResult result)
        {
            // 2020/4/25 KUKA 张建平，朱德清要求进站永远OK, 出站解绑托盘
            result.ErrorCode = ACCCode.ACCOK;
            result.ErrorMessage = string.Empty;

            IsReport = false; // 进站Load时已报工
            return base.SendUnloadResult(dev, result);
        }
    }
}